import type {ChatMessage} from "./message.ts";


/**
 * 群成员
 */
class GroupMember {
    userId: string;
    // 用户在群中的别名
    aliasName: string;
    // 昵称
    nickname: string;
    // 头像
    avatarUrl: string;
    // 性别
    gender: number;

    showName: string;

    constructor(
        userId: string,
        aliasName: string,
        nickname: string,
        avatarUrl: string,
        gender: number
    ) {
        this.userId = userId;
        this.aliasName = aliasName;
        this.nickname = nickname;
        this.avatarUrl = avatarUrl;
        this.gender = gender;
        this.showName = this.aliasName ? this.aliasName : this.nickname;
    }
}


/**
 * 群组实体
 */
class GroupItem {
    // 主键ID
    relId: string;
    // 群号，用于搜索、添加成员等
    groupNo: string;
    // 群名称
    name: string;
    // 群头像
    avatarUrl: string;
    // 群组成员list
    users: GroupMember[] = [];

    constructor(
        relId: string,
        groupNo: string,
        name: string,
        avatarUrl: string,
        users: GroupMember[] = []
    ) {
        this.relId = relId;
        this.groupNo = groupNo;
        this.name = name;
        this.avatarUrl = avatarUrl;
        this.users = users;
    }
}

/**
 * 联系人实体
 */
class ContactsItem {
    relId: string; // 聊天关系ID
    // 联系人ID
    userId: string;
    // 备注名
    aliasName: string;
    // 账号
    username: string;
    // 头像
    avatarUrl: string;
    // 昵称
    nickname: string;
    // 性别
    gender: number;
    // 个人简介
    profile: string;
    // 首字母
    firstLetter: string;
    isFriend: boolean;

    constructor(
        relId: string,
        userId: string,
        aliasName: string,
        username: string,
        avatarUrl: string,
        nickname: string,
        gender: number,
        profile: string,
        firstLetter: string
    ) {
        this.relId = relId;
        this.userId = userId;
        this.aliasName = aliasName;
        this.username = username;
        this.avatarUrl = avatarUrl;
        this.nickname = nickname;
        this.gender = gender;
        this.profile = profile;
        this.firstLetter = firstLetter;
        this.isFriend = true;
    }

}

/**
 * User实体，仅用来表示当前用户
 */
class User {
    uid: string;
    username: string;
    nickname: string; //用户名称
    avatarUrl: string; // 头像

    constructor({ uid, username, nickname, avatarUrl }: {
        uid: string;
        username: string;
        nickname: string;
        avatarUrl: string;
    }) {
        this.uid = uid;
        this.username = username;
        this.nickname = nickname;
        this.avatarUrl = avatarUrl;
    }
}

/**
 * 聊天会话实体
 */
class Chatting {
    user: User; //当前user

    relId: string; //聊天关系ID（个人聊天关系ID or 群组ID）

    showName: string; //显示的名称（一般是nickname或aliasName）
    avatarUrl: string; // 头像

    isGroup: boolean; //表示是不是群组聊天

    unreadCount: number; //未读的消息数量
    messages: ChatMessage[]; //消息列表
    lastTime?: string; //最后聊天的时间
    lastTip?: string; //列表中的消息展示
    online: boolean = false //是否在线（只针对个人聊天）
    peerUserId: string = ""; //对方的userId（只针对个人聊天）

    constructor(user: User, relId: string, showName: string, avatarUrl: string, isGroup: boolean) {
        this.user = user;
        this.showName = showName;
        this.avatarUrl = avatarUrl;
        this.unreadCount = 0;
        this.messages = [];
        this.lastTime = "";
        this.lastTip = "";
        this.relId = relId;
        this.isGroup = isGroup;
        // this.relId = this.chatter.relId;
        // this.isGroup = ("groupNo" in chatter);
        // this.showName = this.isGroup ? (this.chatter as GroupItem).name : ((this.chatter as ContactsItem).aliasName?(this.chatter as ContactsItem).aliasName:(this.chatter as ContactsItem).nickname)
    }
}

export {
    GroupMember,
    GroupItem,
    ContactsItem,
    Chatting,
    User,
}